package com.lianwei.hospital.controller;

import com.lianwei.hospital.Service.IllInfoService;
import com.lianwei.hospital.Service.Impl.IllInfoImpl;
import com.lianwei.hospital.module.entity.IllInfo;
import com.lianwei.hospital.module.vo.IllVO;
import com.lianwei.hospital.utils.PageUtil;
import com.lianwei.hospital.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/ill")
public class IllInfoController {
    @Autowired
    private IllInfoService illInfoService;

    @GetMapping("/selectById")
    public Result<IllInfo> Select(String i) throws Exception {
        System.out.println(i+"\n\n");
        IllInfo illInfo = illInfoService.selectByPrimaryKey(i);
        return Result.buildResult(Result.Status.OK,illInfo);
    }

    @PutMapping
    public Result<List<IllInfo>> Insert(@RequestBody IllInfo illInfo) throws Exception {
        System.out.println(illInfo);
        if(illInfoService.insert(illInfo)){
            List<IllInfo> illInfoList = illInfoService.selectAll();
            return Result.buildResult(Result.Status.OK,illInfoList);
        }
        return Result.buildResult(Result.Status.BAD_REQUEST);
    }

    @DeleteMapping
    public Result<List<IllInfo>> Delete(@RequestBody IllInfo illInfo) throws Exception {
        System.out.println(illInfo);
        if(illInfoService.deleteByPrimaryKey(String.valueOf(illInfo.getId()))){
            List<IllInfo> illInfoList = illInfoService.selectAll();
            return Result.buildResult(Result.Status.OK,illInfoList);
        }
        return Result.buildResult(Result.Status.BAD_REQUEST);
    }

    @PostMapping
    public Result<List<IllInfo>> Update(@RequestBody IllInfo illInfo) throws Exception {
        System.out.println(illInfo);
        if(illInfoService.updateByPrimaryKey(illInfo)){
            List<IllInfo> illInfoList = illInfoService.selectAll();
            return Result.buildResult(Result.Status.OK,illInfoList);
        }
        return Result.buildResult(Result.Status.BAD_REQUEST);
    }

    @GetMapping("/selectAll")
    public Result<PageUtil<IllInfo>> list(@RequestBody IllVO query) throws Exception {
        PageUtil<IllInfo> pageUtil= illInfoService.findPage(query);
        return Result.buildResult(Result.Status.OK,pageUtil);
    }
}
